package service;

import dao.PetDao;
import dao.PetDaoMySQLImpl;
import dao.PetDaoOracleImpl;
import entity.Pet;
import factory.PetDaoFactory;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2024/11/29  11:28
 */
@Service("petService")
public class PetServiceImpl implements PetService {
    //创建PetDao实现类对象
    //PetDao petDao=new PetDaoMySQLImpl();
    //通过指定的名称获取PetDao实例
    //PetDao petDao= PetDaoFactory.getInstance(PetDaoFactory.defaltBeanName);
    //使用注解注入属性时，可以不提供setter方法
    @Resource(type = PetDaoOracleImpl.class)
    private PetDao petDao;
//    public PetDao getPetDao(){
//        return petDao;
//    }
//    public void setPetDao(PetDao petDao){
//        this.petDao = petDao;
//    }
    @Override
    public boolean save(Pet pet) {
        int rows=petDao.insert(pet);
        if (true){
            //模拟业务执行过程中出现异常
            throw new RuntimeException("业务操作异常！");
        }
        return rows>0;
    }

    @Override
    public boolean modify(Pet pet) {
        delay(2000); //模拟执行业务2秒
        return false;
    }

    @Override
    public boolean del(Integer id) {
        delay(1000); //模拟执行业务1秒
        return false;
    }

    private void delay(int time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
